package com.estronger.shopmanage.ui.order.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.order.bean.BitmapItem;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by DingYG on 2017/1/17 0017.
 */

public class AddImagesAdapter extends BaseAdapter{

    protected Context context;
    protected LayoutInflater inflater;
    protected List<BitmapItem> myList = new ArrayList<BitmapItem>();
    protected View.OnClickListener listener;

    public AddImagesAdapter(Context context,View.OnClickListener listener) {
        this.context = context;
        this.inflater =LayoutInflater.from(context);
        this.listener = listener;
    }

    public void appendData(List<BitmapItem> data,boolean isClearOld){
        if(data==null){
            return;
        }else if(isClearOld){
            myList.clear();
        }
        myList.addAll(data);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(myList ==null){
            return 0;
        }else if(myList.size() > 99){
            return 99;
        }
        return myList.size();
    }

    @Override
    public BitmapItem getItem(int position) {
        // TODO Auto-generated method stub
        if(myList ==null||myList.size()<=0){
            return null;
        }
        if(position>myList.size()){
            return null;
        }
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        BitmapItem bitmapItem = myList.get(position);
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.view_orderbitmap, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        try {
            //本地文件
            File file = null;
            if(bitmapItem.getbPath()!=null){
                file = new File(bitmapItem.getbPath());
            }
            //加载图片
            Picasso.with(context).load(file).placeholder(R.drawable.upload_img_icon).
                    error(R.drawable.upload_img_icon).into(vh.imgf);
        }catch (Exception e){}
        int deleteS = bitmapItem.getDelteState();
        if(deleteS == 0){
            vh.imgfDelete.setVisibility(View.GONE);
        }else{
            vh.imgfDelete.setVisibility(View.VISIBLE);
            vh.imgfDelete.setTag(position);
            vh.imgfDelete.setOnClickListener(listener);
        }
        return convertView;
    }
    class ViewHolder{
        private ImageView imgf;
        private ImageView imgfDelete;
        public ViewHolder(View view){
            this.imgf = (ImageView) view.findViewById(R.id.imgfCpBitmap);
            this.imgfDelete = (ImageView) view.findViewById(R.id.imgfDelete);
        }
    }

}
